package com.excel.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.excel.converter.ObjConverter;
import com.excel.entity.*;
import com.excel.enums.ImportTypeEnum;
import com.excel.enums.LanguageTypeEnum;
import com.excel.executors.BizExecutor;
import com.excel.util.ExcelUtil;
import com.excel.util.FuncUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author User
 */
@Slf4j
@Service
public class CommonExcelService {

    @Resource
    private ImportService importService;

    @Resource
    private ObjConverter objConverter;


    public ImportInfoVO importObj(MultipartFile file, EvaluateObjAddReq addReq) {
        List<? extends BaseExcelData> evaluateObjImportExcelDataList;
        try {
            importService.checkImportHead(file.getInputStream(), ImportTypeEnum.USER_AUTH);
            evaluateObjImportExcelDataList = getObjImportExcelData(file);
        } catch (IOException e) {
            log.error("导入失败:{}", e);
            throw new RuntimeException("导入失败");
        }
        FuncUtil.throwIfTrue(CollUtil.isEmpty(evaluateObjImportExcelDataList), "EVAL_OBJ_IMPORT_DATA_EMPTY_ERROR");
        ImportInfo importInfo = importService.saveImportInfo(
                file,
                evaluateObjImportExcelDataList.size(),
                JSONUtil.toJsonStr(addReq),
                ImportTypeEnum.USER_AUTH.name());
        BizExecutor.getPool().execute(() -> importService.importData(importInfo));
        return objConverter.convertImportInfoVO(importInfo);

    }

    private List<? extends BaseExcelData> getObjImportExcelData(MultipartFile file) throws IOException {
//        LanguageTypeEnum typeEnum = NeptuneUtil.getCurrentLanguageType();
//        if (typeEnum.equals(LanguageTypeEnum.EN_US)) {
//            return ExcelUtil.importExcel(file.getInputStream(), 0, 1, true,
//                    EvaluateObjImportExcelEnData.class);
//        }
        return ExcelUtil.importExcel(file.getInputStream(), 0, 1, true,
                EvaluateObjImportExcelData.class
        );
    }

    public Collection<String> checkAndSaveExcelData(BaseExcelData excelData, String createUser, String parameter) {
        List<String> errorList = new ArrayList<>();
        return errorList;
    }

    public ImportData getCommonExcelData(InputStream in, LanguageTypeEnum languageType) {
        try {
            if (languageType == LanguageTypeEnum.EN_US) {
//                List<DimensionImportExcelDataEn> enList = ExcelUtil.importExcel(in, 0,
//                        1, true,
//                        DimensionImportExcelDataEn.class
//                );
//                return new ImportData(enList);
            }
            List<EvaluateObjImportExcelData> detailList = ExcelUtil.importExcel(in, 0, 1, true,
                    EvaluateObjImportExcelData.class
            );
            return new ImportData(detailList);
        } catch (Exception e) {
            throw new RuntimeException("CustomErrorCodes.DIMENSION_IMPORT_ERROR");
        }
    }
}
